using System;
using System.Collections;
using System.Collections.Generic;

public class Tools
{
    public static long NowTime()
    {
        return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
    } 

    public static long NowMillisecTime()
    {
        return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
    } 

    public static ulong Bytes2ULong(byte[] buffer,int offset,int size,bool isLittleEndian)
    {
        if(BitConverter.IsLittleEndian != isLittleEndian)
        {
            ReverseBytes(buffer,offset,size);
        }
        
        ulong n = 0;
        if(BitConverter.IsLittleEndian)
        {
            for(int i = size - 1; i >= 0; i--)
            {
                n = (n << 8) + (ulong)buffer[offset + i];
            }
        }
        else
        {
            for(int i = 0; i < size; i++)
            {
                n = (n << 8) + (ulong)buffer[offset + i];
            }
        }
        return n;
    }

    public static void ULongBytes(ulong n,byte[] buffer,int offset,int size,bool isLittleEndian)
    {
        ulong sn = n;

        if(BitConverter.IsLittleEndian)
        {
            for(int i = 0; i < size; i++)
            {
                byte d = (byte)(sn & 0xff);
                buffer[offset + i] = d;
                sn = sn >> 8;
            }
        }
        else
        {
            for(int i = size - 1; i >= 0; i--)
            {
                byte d = (byte)(sn & 0xff);
                buffer[offset + i] = d;
                sn = sn >> 8;
            }
        }

        if(BitConverter.IsLittleEndian != isLittleEndian)
        {
            ReverseBytes(buffer,offset,size);
        }
    }

    public static void ReverseBytes(byte[] buffer,int offset,int size)
    {
        int count = size / 2;
        for(int i = 0; i < count; i++)
        {
            byte temp = buffer[offset + i];
            buffer[offset + i] = buffer[offset + size - i - 1];
            buffer[offset + size - i - 1] = temp;
        }
    } 

    public static void MoveBytes(byte[] buffer,int startIndex,int moveIndex,int count)
    {
        if(startIndex >= moveIndex)
        {
            for(int i = 0; i < count; i++)
            {
                buffer[moveIndex + i] = buffer[startIndex + i];
            }
        }
        else
        {
            for(int i = count - 1; i >= 0; i--)
            {
                buffer[moveIndex + i] = buffer[startIndex + i];
            }
        }
    }
}